package cn.hpclub.server.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.jfinal.kit.StrKit;

public class CallBackData{
    private static Logger logger = Logger.getLogger(CallBackData.class.getName());
    private final String  order_id;
    private final String  thirdpart_id;
    private final String  amount;
    private final String  phone_no;
    private final String  return_code;
    private final String  return_msg;
    private final String  storeid;
    private final String  userid;
    private final String  time;
    private final String  business_code;
    private final String  systemid;
    private final String  source;
    private final String  saletype;
    private final String  methodName;
    private final String  moduleCode;

    private final String  agent;
    private final String  moduleId;
    private final String  isUnreg;

    private final String  business_name;                                          // 1.5.9版本新增

    public static class Builder{

        private String order_id;
        private String thirdpart_id;
        private String amount   = "0";
        private String phone_no;
        private String return_code;
        private String return_msg;
        private String storeid;
        private String userid;
        private String time;
        private String business_code;
        private String systemid = "1";
        private String source   = "0";
        private String saletype = "1";
        private String methodName;
        private String moduleCode;

        //
        private String agent;
        private String moduleId;
        private String isUnreg;

        private String business_name;

        public Builder(){

        }

        public Builder moduleCode(String moduleCode){
            this.moduleCode = moduleCode;
            return this;
        }

        public Builder order_id(String order_id){
            this.order_id = order_id;
            return this;
        }

        public Builder thirdpart_id(String thirdpart_id){
            this.thirdpart_id = thirdpart_id;
            return this;
        }

        public Builder amount(String amount){
            this.amount = amount;
            return this;
        }

        public Builder phone_no(String phone_no){
            this.phone_no = phone_no;
            return this;
        }

        public Builder return_code(String return_code){
            if(return_code.equals("0")){
                this.return_code = "0";
            } else{
                this.return_code = "1";
            }
            return this;
        }

        public Builder return_msg(String return_msg){
            if(return_msg == null){
                return_msg = "";
            }
            this.return_msg = return_msg;
            return this;
        }

        public Builder storeid(String storeid){
            this.storeid = storeid;
            return this;
        }

        public Builder userid(String userid){
            this.userid = userid;
            return this;
        }

        public Builder time(String time){
            if(time == null){
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                time = format.format(new Date(System.currentTimeMillis()));

            }
            this.time = time;
            return this;
        }

        public Builder business_code(String business_code){
            this.business_code = business_code;
            return this;
        }

        /**
         * 1.5.9版本新增，保存资费名称到sales_promotion表
         * 
         * @Title: business_name
         * @Description:TODO
         * @param business_name
         * @return Builder
         */
        public Builder business_name(String business_name){
            this.business_name = business_name;
            return this;
        }

        public Builder moduleId(String moduleId){
            this.moduleId = moduleId;
            return this;
        }

        public Builder isUnreg(String isUnreg){
            this.isUnreg = isUnreg;
            return this;
        }

        public Builder agent(String agent){
            this.agent = agent;
            return this;
        }

        public Builder systemid(String systemid){
            this.systemid = systemid;
            return this;
        }

        public Builder source(String source){
            this.source = source;
            return this;
        }

        public Builder saletype(String saletype){
            this.saletype = saletype;
            return this;
        }

        public Builder methodName(String method){
            this.methodName = method;
            return this;
        }

        public CallBackData build(){

            if(phone_no == null){
                System.out.println("phone_no is null");
                throw new RuntimeException("Return code is null");
            }

            if(return_code == null){
                System.out.println("Return code is null");
                throw new RuntimeException("Return code is null");
            }

            if(storeid == null){
                System.out.println("Store id is null");
                throw new RuntimeException("Store id is null");
            }

            if(userid == null){
                System.out.println("User id is null");
                throw new RuntimeException("User id is null");
            }

            if(business_code == null){
                System.out.println("Business code is null");
                throw new RuntimeException("Business code is null");
            }

            if(isUnreg == null){
                System.out.println("IsUnreg is null");
                throw new RuntimeException("IsUnreg is null");
            }

            if(moduleId == null){
                System.out.println("Module Id is null");
                throw new RuntimeException("Module Id is null");
            }

            if(methodName == null){
                System.out.println("methodName  is null");
                throw new RuntimeException("methodName  is null");
            }

            if(business_name == null){
                System.out.println("Business name is null");
                throw new RuntimeException("Business name is null");
            }

            return new CallBackData(this);
        }

    }

    private CallBackData(Builder builder){
        this.order_id = builder.order_id;
        this.thirdpart_id = builder.thirdpart_id;

        if(builder.time == null){
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            this.time = format.format(new Date(System.currentTimeMillis()));

        } else{
            this.time = builder.time;
        }

        this.amount = builder.amount;
        this.phone_no = builder.phone_no;
        this.return_code = builder.return_code;
        this.return_msg = builder.return_msg;
        this.storeid = builder.storeid;
        this.userid = builder.userid;
        this.business_code = builder.business_code;
        this.systemid = builder.systemid;
        this.source = builder.source;
        this.saletype = builder.saletype;
        this.agent = builder.agent;
        this.moduleId = builder.moduleId;
        this.moduleCode = builder.moduleCode;
        this.isUnreg = builder.isUnreg;
        this.methodName = builder.methodName;
        this.business_name = builder.business_name;
    }

    public String toXml(){
        // Map<String, Object> rootMap = new LinkedHashMap<String, Object>();
        Map<String, Object> dataMap = new LinkedHashMap<String, Object>();

        dataMap.put("order_id", order_id);
        dataMap.put("thirdpart_id", thirdpart_id);
        dataMap.put("amount", amount);
        dataMap.put("phone_no", phone_no);
        dataMap.put("return_code", return_code);
        dataMap.put("return_msg", return_msg);
        dataMap.put("storeid", storeid);
        dataMap.put("userid", userid);
        dataMap.put("time", time);
        dataMap.put("business_code", business_code);
        dataMap.put("foreign_content", createForeign_content());
        dataMap.put("systemid", systemid);
        dataMap.put("source", source);
        dataMap.put("saletype", saletype);
        dataMap.put("methodName", methodName);
        dataMap.put("business_name", business_name);
        // rootMap.put("data", dataMap);
        // rootMap.put("sign", "");
        String xml = map2Xml(dataMap);
        // logger.info(xml);
        return xml;
    }

    private String map2Xml(Map<String, Object> dataMap){

        Document document = DocumentHelper.createDocument();
        // document.setXMLEncoding("GB2312");

        Element rootElement = document.addElement("root");
        Element dataElement = rootElement.addElement("data");

        // System.out.println("param str:");
        for(Iterator iter = dataMap.keySet().iterator(); iter.hasNext();){
            String key = (String)iter.next();
            String value = (String)dataMap.get(key);

            Element keyValueElement = dataElement.addElement(key);
            keyValueElement.setText(value);

        }

        rootElement.addElement("sign");

        return document.asXML();
    }

    public String createForeign_content(){
        StringBuilder sb = new StringBuilder();
        sb.append("agent=");
        sb.append((this.agent == null) ? "" : this.agent);
        sb.append("_");
        sb.append("isUnreg=");
        sb.append(this.isUnreg);
        sb.append("_");
        sb.append("moduleId=");
        sb.append(this.moduleId);
        if(StrKit.notBlank(moduleCode)){
            sb.append("_");
            sb.append("moduleCode =");
            sb.append(this.moduleCode);
        }
        return sb.toString();
    }

}
